
<template>
  <page-header-wrapper :title="false">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="16">
            <!-- <a-col :span="8">
              <a-form-item label="分类名称">
                <a-input v-model="queryParam.name" placeholder="请输入" />
              </a-form-item>
            </a-col> -->
            <a-col :span="8" :offset="16" style="text-align: right;">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
                <!-- <a-button type="primary" style="margin-left: 8px" @click="handleSearch">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :pageNum="pageNum"
        :pageSize="pageSize"
        rowKey="id">
        <div slot="action" slot-scope="text, record">
          <a type="primary" :disabled="record.state === 1" @click="handleStatus(record)">激活</a>
          <a
            :disabled="record.state === 2"
            @click="handleStatus(record)"
            style="margin-left:10px;">冻结</a>
          <a
            @click="handleEdit(record)"
            type="primary"
            style="margin-left:10px;">更新</a>
        </div>
      </s-table>
    </a-card>
    <create-form
      ref="createModal"
      :visible="visible"
      :loading="confirmLoading"
      :model="mdl"
      @cancel="handleCancel"
      @ok="handleOk" />
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import { goodCategoryList as list, goodCategoryFrozen as frozen, goodCategoryActive as active, goodCategoryAdd as add, goodCategoryUpdate as update } from '@/api/shop'
import CreateForm from './modules/category-form'
export default {
  name: 'TableList',
  components: {
    STable,
    CreateForm
  },
  // status 1-激活 2-冻结 3-删除
  data () {
    return {
      queryParam: {},
      pageNum: 1,
      pageSize: 20,
      visible: false,
      mdl: null,
      confirmLoading: false,
      columns: [
        {
          title: '所属店铺',
          dataIndex: 'shopsIdName'
        },
        {
          title: '类目名称',
          dataIndex: 'name'
        },
        {
          title: '排序',
          dataIndex: 'sortOrder'
        },
        {
          title: '操作',
          width: 140,
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ]
    }
  },
  created () {
  },
  methods: {
    handleSearch (e) {
      this.$refs.table.refresh()
    },
    handleEdit (record) {
      // console.log(record)
      this.visible = true
      this.mdl = { ...record }
    },
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleCancel () {
      this.visible = false
      this.$refs.createModal.fileList = []
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleReset () {
      this.queryParam = {}
      this.$refs.table.refresh()
    },
    loadData (params) {
      return list(Object.assign(params, this.queryParam)).then(res => {
        return res.data
      })
    },
    handleStatus (row) {
      const { state, id } = row
      this.$confirm({
        title: `您确定要${state === 1 ? '冻结' : '激活'}该商品分类吗？`,
        onOk: () => {
          if (row.state === 1) {
            frozen([id]).then(() => {
              this.$message.success('冻结成功')
              this.$refs.table.refresh()
            })
          } else {
            active([id]).then(() => {
              this.$message.success('激活成功')
              this.$refs.table.refresh()
            })
          }
        }
      })
    },
    handleOk () {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          if (values.id > 0) {
            // 修改 e.g.
            update(values).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()
              this.$message.info('修改成功')
            })
          } else {
            // 新增
            add(values).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.info('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    }
  }
}
</script>
